/*
========================================================================
 Name        : Test9ListBoxView.cpp
 Author      : 
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <aknviewappui.h>
#include <eikmenub.h>
#include <avkon.hrh>
#include <barsread.h>
#include <stringloader.h>
#include <aknlists.h>
#include <eikenv.h>
#include <akniconarray.h>
#include <eikclbd.h>
#include <akncontext.h>
#include <akntitle.h>
#include <eikbtgpc.h>
#include <aknquerydialog.h>
#include <Test9.rsg>
// ]]] end generated region [Generated System Includes]
#include "Test9AppUi.h"
// [[[ begin generated region: do not modify [Generated User Includes]

#include "Test9.hrh"
#include "Test9ListBoxView.h"
#include "Test9Container.hrh"
#include "Test9ListBox.hrh"
#include "Test9Container2.hrh"
#include "Test9ListBox.h"
// ]]] end generated region [Generated User Includes]
#include "Settings.h"
// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]
#include <eikdll.h>
/**
 * First phase of Symbian two-phase construction. Should not contain any
 * code that could leave.
 */
CTest9ListBoxView::CTest9ListBoxView()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	iTest9ListBox = NULL;
	// ]]] end generated region [Generated Contents]
	
	}

/** 
 * The view's destructor removes the container from the control
 * stack and destroys it.
 */
CTest9ListBoxView::~CTest9ListBoxView()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	delete iTest9ListBox;
	iTest9ListBox = NULL;
	// ]]] end generated region [Generated Contents]
	
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance then calls the second-phase constructor
 * without leaving the instance on the cleanup stack.
 * @return new instance of CTest9ListBoxView
 */
CTest9ListBoxView* CTest9ListBoxView::NewL()
	{
	CTest9ListBoxView* self = CTest9ListBoxView::NewLC();
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance, pushes it on the cleanup stack,
 * then calls the second-phase constructor.
 * @return new instance of CTest9ListBoxView
 */
CTest9ListBoxView* CTest9ListBoxView::NewLC()
	{
	CTest9ListBoxView* self = new ( ELeave ) CTest9ListBoxView();
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}


/**
 * Second-phase constructor for view.  
 * Initialize contents from resource.
 */ 
void CTest9ListBoxView::ConstructL()
	{
	// [[[ begin generated region: do not modify [Generated Code]
	BaseConstructL( R_TEST9_LIST_BOX_TEST9_LIST_BOX_VIEW );
				
	// ]]] end generated region [Generated Code]
	
	// add your own initialization code here
	
	}

/**
 * @return The UID for this view
 */
TUid CTest9ListBoxView::Id() const
	{
	return TUid::Uid( ETest9ListBoxViewId );
	}

/**
 * Handle a command for this view (override)
 * @param aCommand command id to be handled
 */
void CTest9ListBoxView::HandleCommandL( TInt aCommand )
	{
	// [[[ begin generated region: do not modify [Generated Code]
	TBool commandHandled = EFalse;
	switch ( aCommand )
		{	// code to dispatch to the AknView's menu and CBA commands is generated here
		case ETest9ListBoxViewOKMenuItemCommand:
			commandHandled = HandleOKMenuItemSelectedL( aCommand );
			break;
		case ETest9ListBoxViewDeleteMenuItemCommand:
			commandHandled = HandleDeleteMenuItemSelectedL( aCommand );
			break;
		default:
			break;
		}
	
	// ]]] end generated region [Generated Code]
	
	if ( !commandHandled ) 
		{
		if ( aCommand == EAknSoftkeyBack )
			{
			AppUi()->HandleCommandL( EAknSoftkeyBack );
			}
	
		if ( aCommand == EAknSoftkeyOk || aCommand == EAknSoftkeySelect || aCommand == EAknSoftkeyOpen)
			{
			SetFileName();
			AppUi()->HandleCommandL( EAknSoftkeyOk );
			}
		}
	
	}

bool CTest9ListBoxView::DeleteFile()
	{
	if(RunConfQuery1L(0) == 0)
		return false;
	
	//1 - Get selected text.
	CTextListBoxModel* model = iTest9ListBox->ListBox()->Model();    // Does not own the returned model
	User::LeaveIfNull(model);
	
	model->SetOwnershipType(ELbmOwnsItemArray);

	CDesCArray* itemArray = static_cast<CDesCArray*>(model->ItemTextArray());
	User::LeaveIfNull(itemArray);
	
	TBuf<50> filename;
	filename.Copy(_L(""));
	filename.Append(itemArray->operator [](iTest9ListBox->ListBox()->CurrentItemIndex()));
	filename.Delete(0, 1);	//remove the tab character at the first of the text (for the icon slot issue)
	
	//2 - Delete the file.
	TBuf<256> KShopFileName;
	if(CSettings::theInstance()->isPhoneMemory)
		KShopFileName.Append(_L("C:\\Data"));
	else
		KShopFileName.Append(_L("E:"));
	KShopFileName.Append(_L("\\Others\\WalkAssist\\"));
	
	KShopFileName.Append(filename);
	
	//3 - Delete file from desk.
	User::LeaveIfError(iEikonEnv->FsSession().Connect());
	User::LeaveIfError(iEikonEnv->FsSession().Delete(KShopFileName));
	
	//4 - Delete item from list.
	iTest9ListBox->DeleteSelectedFile();

	return true;
	}

bool CTest9ListBoxView::SetFileName()
	{
	//1 - Get Selected Text.
	CTextListBoxModel* model = iTest9ListBox->ListBox()->Model();    // Does not own the returned model
	User::LeaveIfNull(model);
	
	model->SetOwnershipType(ELbmOwnsItemArray);

	CDesCArray* itemArray = static_cast<CDesCArray*>(model->ItemTextArray());
	User::LeaveIfNull(itemArray);
	
	TBuf<50> filename;
	filename.Copy(_L(""));
	filename.Append(itemArray->operator [](iTest9ListBox->ListBox()->CurrentItemIndex()));
	filename.Delete(0, 1);	//remove the tab character at the first of the text (for the icon slot issue)
	
	//2 - Set filename.
	((CTest9AppUi*)iEikonEnv->AppUi())->iTest9Container2View->strFilename = filename;
	
	return true;
	}


/**
 *	Handles user actions during activation of the view, 
 *	such as initializing the content.
 */
void CTest9ListBoxView::DoActivateL( 
		const TVwsViewId& /*aPrevViewId*/,
		TUid /*aCustomMessageId*/,
		const TDesC8& /*aCustomMessage*/ )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	SetupStatusPaneL();
	
				
	if ( iTest9ListBox == NULL )
		{
		iTest9ListBox = CreateContainerL();
		iTest9ListBox->SetMopParent( this );
		AppUi()->AddToStackL( *this, iTest9ListBox );
		} 
	// ]]] end generated region [Generated Contents]
	
	}

/**
 */
void CTest9ListBoxView::DoDeactivate()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	CleanupStatusPane();
	
	if ( iTest9ListBox != NULL )
		{
		AppUi()->RemoveFromViewStack( *this, iTest9ListBox );
		delete iTest9ListBox;
		iTest9ListBox = NULL;
		}
	// ]]] end generated region [Generated Contents]
	
	}

/** 
 * Handle status pane size change for this view (override)
 */
void CTest9ListBoxView::HandleStatusPaneSizeChange()
	{
	CAknView::HandleStatusPaneSizeChange();
	
	// this may fail, but we're not able to propagate exceptions here
	TVwsViewId view;
	AppUi()->GetActiveViewId( view );
	if ( view.iViewUid == Id() )
		{
		TInt result;
		TRAP( result, SetupStatusPaneL() );
		}
	
	// [[[ begin generated region: do not modify [Generated Code]
	// ]]] end generated region [Generated Code]
	
	}

// [[[ begin generated function: do not modify
void CTest9ListBoxView::SetupStatusPaneL()
	{
	// reset the context pane
	TUid contextPaneUid = TUid::Uid( EEikStatusPaneUidContext );
	CEikStatusPaneBase::TPaneCapabilities subPaneContext = 
		StatusPane()->PaneCapabilities( contextPaneUid );
	if ( subPaneContext.IsPresent() && subPaneContext.IsAppOwned() )
		{
		CAknContextPane* context = static_cast< CAknContextPane* > ( 
			StatusPane()->ControlL( contextPaneUid ) );
		context->SetPictureToDefaultL();
		}
	
	// setup the title pane
	TUid titlePaneUid = TUid::Uid( EEikStatusPaneUidTitle );
	CEikStatusPaneBase::TPaneCapabilities subPaneTitle = 
		StatusPane()->PaneCapabilities( titlePaneUid );
	if ( subPaneTitle.IsPresent() && subPaneTitle.IsAppOwned() )
		{
		CAknTitlePane* title = static_cast< CAknTitlePane* >( 
			StatusPane()->ControlL( titlePaneUid ) );
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_TEST9_LIST_BOX_TITLE_RESOURCE );
		title->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
				
	}

// ]]] end generated function

// [[[ begin generated function: do not modify
void CTest9ListBoxView::CleanupStatusPane()
	{
	}

// ]]] end generated function

/**
 *	Creates the top-level container for the view.  You may modify this method's
 *	contents and the CTest9ListBox::NewL() signature as needed to initialize the
 *	container, but the signature for this method is fixed.
 *	@return new initialized instance of CTest9ListBox
 */
CTest9ListBox* CTest9ListBoxView::CreateContainerL()
	{
	return CTest9ListBox::NewL( ClientRect(), NULL, this );
	}

/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CTest9ListBoxView::HandleOpen_FolderMenuItemSelectedL( TInt aCommand )
	{
	// TODO: implement selected event handler
	_LIT(KAppFileName, "c:\\System\\FileManager.exe");
	//EikDll::StartExeL(KAppFileName);
	
	_LIT(KExeFileName, "File manager"); //java midp engine
	_LIT(KCmdLine, ""); //jad to laucnh .jar

	RProcess launchProcess;
	if (launchProcess.Create(KExeFileName, KCmdLine) == KErrNone)
	{
	launchProcess.Resume();
	}
	
	return ETrue;
	}

void CTest9ListBoxView::DynInitMenuPaneL(TInt aResourceId, CEikMenuPane* aMenuPane)
	{
	if (R_TEST9_LIST_BOX_MENU_PANE1_MENU_PANE == aResourceId)
		{
		if(iTest9ListBox->ListBox()->Model()->NumberOfItems() == 0)
			{
			aMenuPane->SetItemDimmed(ETest9ListBoxViewOKMenuItemCommand, ETrue);
			aMenuPane->SetItemDimmed(ETest9ListBoxViewDeleteMenuItemCommand, ETrue);
			}
		else
			{
			aMenuPane->SetItemDimmed(ETest9ListBoxViewOKMenuItemCommand, EFalse);
			aMenuPane->SetItemDimmed(ETest9ListBoxViewDeleteMenuItemCommand, EFalse);
			}
		}
	}
				
/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CTest9ListBoxView::HandleOKMenuItemSelectedL( TInt aCommand )
	{
	// TODO: implement selected event handler
	HandleCommandL(EAknSoftkeyOk);
	return ETrue;
	}

/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CTest9ListBoxView::HandleDeleteMenuItemSelectedL( TInt aCommand )
	{
	// TODO: implement selected event handler
	DeleteFile();
	return ETrue;
	}
				
// [[[ begin generated function: do not modify
/**
 * Show the popup dialog for confQuery1
 * @param aOverrideText optional override text
 * @return EAknSoftkeyYes (left soft key id) or 0
 */
TInt CTest9ListBoxView::RunConfQuery1L( const TDesC* aOverrideText )
	{
				
	CAknQueryDialog* queryDialog = CAknQueryDialog::NewL();	
	
	if ( aOverrideText != NULL )
		{
		queryDialog->SetPromptL( *aOverrideText );
		}
	return queryDialog->ExecuteLD( R_TEST9_LIST_BOX_CONF_QUERY1 );
	}
// ]]] end generated function

